#!/bin/bash -ex

# Settings
PAGE_REPO_PATH=git@github.com:logcabin/logcabin.github.io.git
HTML_PATH=docs/doxygen
PAGE_PATH=logcabin.github.io
DOCS_DIR=doxygen
PAGE_BRANCH=master
COMMIT_USER="Documentation Builder"
COMMIT_EMAIL="ongardie+automated@gmail.com"
CHANGESET=$(git rev-parse --short HEAD)
CHANGESET_LINK=logcabin/logcabin@${CHANGESET}

# Get a clean version of the HTML documentation repo.
rm -rf ${PAGE_PATH}
mkdir -p ${PAGE_PATH}
git clone -b ${PAGE_BRANCH} "${PAGE_REPO_PATH}" --single-branch ${PAGE_PATH}

# rm all the files through git to prevent stale files.
cd ${PAGE_PATH}
git rm -rf --ignore-unmatch ${DOCS_DIR}
cd -

# Copy over the HTML documentation.
cp -av ${HTML_PATH} ${PAGE_PATH}

# Create and commit the documentation repo.
cd ${PAGE_PATH}
git add ${DOCS_DIR}
git diff --exit-code --quiet HEAD && exit 0 # no changes
git config user.name "${COMMIT_USER}"
git config user.email "${COMMIT_EMAIL}"
git commit -m "Automated documentation build for ${CHANGESET_LINK}"
git push origin ${PAGE_BRANCH}
cd -
